package com.seeyon.ctp.common.usermessage.pipeline;

import java.io.Serializable;
import java.util.Date;

import com.seeyon.ctp.common.SystemEnvironment;
import com.seeyon.ctp.common.constants.Constants;
import com.seeyon.ctp.common.usermessage.MessageContent;
import com.seeyon.ctp.common.usermessage.UserMessageUtil;
import com.seeyon.ctp.organization.bo.V3xOrgMember;
import com.seeyon.ctp.util.LightWeightEncoder;
import com.seeyon.ctp.util.Strings;

/**
 * 
 * 
 * @author <a href="mailto:tanmf@seeyon.com">Tanmf</a>
 * @version 1.0 2008-8-29
 */
public class Message implements Serializable{
	
	private V3xOrgMember senderMember;

	private V3xOrgMember receiverMember;

	private int category;

	private int type;

	private int openType;

	private String content;


	/**
	 * 正文内容，用于转发邮件
	 */
	private String bodyFinalContent;
	private String bodyContent;
	private String bodyType;
	private Date bodyCreateDate;

	private Long referenceId;

	private Date createDate;

	private String linkType;

	private String[] linkParams;
	
	private boolean receiverOnline = true;
	
	private Boolean isRead = false;
	
	private Long userHistoryMessageId;
	
	private Integer importmentLevel;
	
	private boolean isForceSMS;

	private Object[] messageFilterArgs;//OA-125118 加这个字段给m3使用
	
	private int isAt=0;//@我的
	private int isReply=0;//回复我的
	private int isTrack=0;//我跟踪的
	private int isTemplate=0;//模板类
	
	//消息来源的类型  0：普通消息，1：AI智能消息
	private int sourceType = 0;
	
	private Long templateId;//模板ID
	
	//kk:客开追加消息关联 MESSAGE_RULE 规则 start
	private Long messageRuleId;	
	
	public Long getMessageRuleId() {
		return messageRuleId;
	}

	public void setMessageRuleId(Long messageRuleId) {
		this.messageRuleId = messageRuleId;
	}
	
	private MessageContent messageContent;
	public MessageContent getMessageContent() {
		return messageContent;
	}

	public void setMessageContent(MessageContent messageContent) {
		this.messageContent = messageContent;
	}
	
	//kk:客开追加消息关联 MESSAGE_RULE 规则 end

	public int getIsAt() {
		return isAt;
	}

	public void setIsAt(int isAt) {
		this.isAt = isAt;
	}

	public int getIsReply() {
		return isReply;
	}

	public void setIsReply(int isReply) {
		this.isReply = isReply;
	}

	public int getIsTrack() {
		return isTrack;
	}

	public void setIsTrack(int isTrack) {
		this.isTrack = isTrack;
	}

	public int getIsTemplate() {
		return isTemplate;
	}

	public void setIsTemplate(int isTemplate) {
		this.isTemplate = isTemplate;
	}

	public Long getTemplateId() {
		return templateId;
	}

	public void setTemplateId(Long templateId) {
		this.templateId = templateId;
	}

	/**
	 * 得到正文
	 * 
	 * @return
	 */
	public String getBodyContent() {
		if(Strings.isBlank(bodyType) || Strings.isBlank(bodyContent) || bodyCreateDate == null){
			return "";
		}
		// TODO
//		if(Constants.EDITOR_TYPE_HTML.equals(bodyType) || (MainbodyType.HTML.getKey()+"").equals(bodyType)){
		if(Constants.EDITOR_TYPE_HTML.equals(bodyType) || ("10").equals(bodyType)){			
			return bodyContent;
		}
/*		else if(bodyFinalContent == null){
			try {
//			    TODO: StrExtractor 对应方法前后后再处理
//				bodyFinalContent = StrExtractor.getText(bodyType, bodyContent, bodyCreateDate);
			}
			catch (Exception e) {
				bodyFinalContent = "";
			}
		}*/
		
		return bodyFinalContent;
	}

	public void setBody(String bodyContent, String bodyType, Date bodyCreateDate){
		this.bodyContent = bodyContent;
		this.bodyType = bodyType;
		this.bodyCreateDate = bodyCreateDate;
	}

	/**
	 * @return 所属应用类别
	 */
	public int getCategory() {
		return category;
	}

	public void setCategory(int category) {
		this.category = category;
	}

	public String getContent() {
		return content;
	}

	public void setContent(String content) {
		this.content = content;
	}

	public Date getCreateDate() {
		return createDate;
	}

	public void setCreateDate(Date createDate) {
		this.createDate = createDate;
	}

	public String[] getLinkParams() {
		return linkParams;
	}

	public void setLinkParams(String... linkParams) {
		this.linkParams = linkParams;
	}

	public String getLinkType() {
		return linkType;
	}

	public void setLinkType(String linkType) {
		this.linkType = linkType;
	}

	/**
	 * @return 消息打开方式
	 */
	public int getOpenType() {
		return openType;
	}

	public void setOpenType(int openType) {
		this.openType = openType;
	}

	public V3xOrgMember getReceiverMember() {
		return receiverMember;
	}

	public void setReceiverMember(V3xOrgMember receiverMember) {
		this.receiverMember = receiverMember;
	}

	public Long getReferenceId() {
		return referenceId;
	}

	public void setReferenceId(Long referenceId) {
		this.referenceId = referenceId;
	}

	/**
	 * 发起者可能是<code>null</code>，比如BBS的匿名讨论
	 * 
	 * @return
	 */
	public V3xOrgMember getSenderMember() {
		return senderMember;
	}
	
	/**
	 * 发起者Id，-1表示没有此人，匿名
	 * 
	 * @return
	 */
	public long getSenderMemberId() {
		return senderMember == null ? -1 : senderMember.getId();
	}

	public void setSenderMember(V3xOrgMember senderMember) {
		this.senderMember = senderMember;
	}
	
	public boolean isForceSMS() {
        return isForceSMS;
    }

    public void setForceSMS(boolean isForceSMS) {
        this.isForceSMS = isForceSMS;
    }

    /**
	 * @return 消息类型：0个人；1系统
	 */
	public int getType() {
		return type;
	}

	public void setType(int type) {
		this.type = type;
	}
	
	private String remoteURL;
	
	private static String URLPrefix = null;
	private static String getURLPrefix(){
		if(URLPrefix == null){
			String a8InternetSiteURL = SystemEnvironment.getInternetSiteURL();
			if(Strings.isBlank(a8InternetSiteURL)){
				URLPrefix = "";
			}
			else{
				URLPrefix = a8InternetSiteURL + SystemEnvironment.getContextPath() + "/thirdpartyController.do?method=access";
			}
		}
		
		return URLPrefix;
	}
	
	/**
	 * 
	 * @return http://***:80/seeyon/thirdparty.do?method=access&enc=Encoder{MID=历史消息ID&L=message.link.***&T=timestamp}&P=**&P=**
	 */
	@SuppressWarnings("deprecation")
	public String getRemoteURL(){
		if(remoteURL != null){
			return remoteURL;
		}
		
		String _URLPrefix = getURLPrefix();
		
		String link = linkType==null ? null : UserMessageUtil.getMessageLinkType().get(linkType);
		
		if(Strings.isBlank(link) || link.toLowerCase().startsWith("javascript:") || Strings.isBlank(_URLPrefix)){
			return null;
		}
		
		StringBuilder url = new StringBuilder();
		
		url.append(_URLPrefix);
		
		String enc = LightWeightEncoder.encodeString("MID=" + this.getUserHistoryMessageId() + "&L=" + linkType + "&T=" + createDate.getTime());
		url.append("&enc=" + java.net.URLEncoder.encode(enc));
		
		if(linkParams != null){
			for (String string : linkParams) {
				url.append("&P=" + string);
			}
		}
		
		remoteURL = url.toString();
		
		return remoteURL;
	}

	/**
	 * 接收者是否在线
	 * 
	 * @return true:在线（即通过PC登录到A8）
	 */
	public boolean isReceiverOnline() {
		return receiverOnline;
	}

	public void setReceiverOnline(boolean receiverOnline) {
		this.receiverOnline = receiverOnline;
	}
	
	/**
	 * 标记消息是否已读
	 * @return true:已读
	 */
	public Boolean getIsRead() {
		return isRead;
	}

	public void setIsRead(Boolean isRead) {
		this.isRead = isRead;
	}
	
	/**
	 * 对应的历史消息
	 * @return
	 */
	public Long getUserHistoryMessageId() {
		return userHistoryMessageId;
	}

	public void setUserHistoryMessageId(Long userHistoryMessageId) {
		this.userHistoryMessageId = userHistoryMessageId;
	}

	public Integer getImportmentLevel() {
		return importmentLevel;
	}

	public void setImportmentLevel(Integer importmentLevel) {
		this.importmentLevel = importmentLevel;
	}

    /**
        HTML(10)
        FORM(20)
        TXT(30)
        OfficeWord(41)
        OfficeExcel(42)
        WpsWord(43)
        WpsExcel(44)
        Pdf(45)
     * @return
     */
    public String getBodyType() {
        return bodyType;
    }

	public Object[] getMessageFilterArgs() {
		return messageFilterArgs;
	}

	public void setMessageFilterArgs(Object[] messageFilterArgs) {
		this.messageFilterArgs = messageFilterArgs;
	}

	public int getSourceType() {
		return sourceType;
	}

	public void setSourceType(int sourceType) {
		this.sourceType = sourceType;
	}
	
}
